<?php
declare(strict_types=1);

namespace SixShop\Message\Controller\Api;

use think\Request;

/**
 * 消息设置API控制器
 */
class MessageSettingController extends BaseController
{
    /**
     * 获取用户消息设置
     */
    public function getUserSetting(Request $request)
    {
        $userId = $this->getUserId();
        if (!$userId) {
            return $this->error('请先登录');
        }
        
        $setting = $this->messageSettingModel->getUserSetting($userId);
        if (!$setting) {
            return $this->error('获取设置失败');
        }
        
        return $this->success('获取成功', $setting);
    }
    
    /**
     * 更新用户消息设置
     */
    public function updateSetting(Request $request)
    {
        $userId = $this->getUserId();
        if (!$userId) {
            return $this->error('请先登录');
        }
        
        $data = [
            'system_notify' => $request->param('system_notify/d', null),
            'activity_notify' => $request->param('activity_notify/d', null),
            'order_notify' => $request->param('order_notify/d', null),
            'logistics_notify' => $request->param('logistics_notify/d', null),
            'private_message' => $request->param('private_message/d', null),
        ];
        
        // 过滤空值
        $data = array_filter($data, function($value) {
            return $value !== null;
        });
        
        if (empty($data)) {
            return $this->error('参数错误');
        }
        
        $result = $this->messageSettingModel->updateSetting($userId, $data);
        if (!$result) {
            return $this->error('更新失败');
        }
        
        return $this->success('更新成功');
    }
}
